<?php 
namespace Home\Controller;
use Think\Controller;

class RoleController extends AuthController
{
	public function index(){return;}

	public function all()
	{
		if(IS_POST){
			$res = D("SysRole")->get_all();
			// $output['result'] = $this->getState("");
			$total = count($res);
			$output['total'] = $total;
			$output['rows'] = $res;	
			// dump($res);exit();
			echo json_encode($output);
		}else{
			$res = D("SysRole")->get_all();
			// dump($res);exit();
			// $output['result'] = $this->getState("");
			$total = count($res);
			$output['total'] = $total;
			$output['rows'] = $res;	
			echo json_encode($output);
			// $output['result'] = $this->getState("5");
			// echo json_encode($output);
		}
	}

	public function get_perm()
	{
		if(IS_POST){
			// $page = I("post.page","1");
			// $rows = I("post.rows","10");
			$res = D("Permission")->get_all($page,$rows,$type);
			// dump($res);exit();
			foreach ($res as $key => $value) {
				foreach ($value as $k => $v) {
					$res2[$k]['id'] = $v['id'];
					$res2[$k]['text'] = $v['permission_name'];
				}		
			}
			echo json_encode($res2);
		}else{
			$res = D("Permission")->get_all();
			// dump($res);
			foreach ($res as $key => $value) {
				foreach ($value as $k => $v) {
					$res2[$k]['id'] = $v['id'];
					$res2[$k]['text'] = $v['permission_name'];
				}		
			}
			// dump($res2);exit();
			echo json_encode($res2);
		}
	}

	public function add()
	{
		if(IS_POST){
			$role_name = I("post.role","");
			$status = I("post.status","");
			$auth = I("post.auth","");
			// $parent_id = I("post.parent_id","");
			if(($role_name) != ''){
				$last_id = D("SysRole")->add_role($role_name,$status);
				// 获取到插入成功的角色id
				if($last_id){
					$res = D("Permission")->get_perm_id($last_id,$auth);
				}
			}
			$res?$output = 1:$output = 0;
			echo json_encode($output);
		}else{
			$role_name = I("post.role","kaka");
			$status = I("post.status","");
			$auth = I("post.auth","增加省份,节目删除");
			// $parent_id = I("post.parent_id","");
			if(($role_name) != ''){
				$last_id = D("SysRole")->add_role($role_name,$status);
				// 获取到插入成功的角色id
				if($last_id){
					$res = D("Permission")->get_perm_id($last_id,$auth);
				}
			}
			$res?$output = 1:$output = 0;
			echo json_encode($output);
			// $output['result'] = $this->getState("5");
			// echo json_encode($output);
		}
	}

	public function delete()
	{
		if(IS_POST){
			$id = I("post.id","");
			if($id){
				$res = D("SysRole")->delete_role($id);
			}	
			$res?$output = 1:$output = 0;
			echo json_encode($output);
		}else{
			// $output['result'] = $this->getState("5");
			// echo json_encode($output);
		}
	}

	public function edit_select()
	{
		if(IS_POST){
			$id = I("post.id","2");
			$res = D("SysRole")->select_role($id);
			$res1 = D("RolePermission")->get_permiss($id);
			if($res && $res1){
				foreach ($res as $k => $v) {
					foreach ($res1 as $key => $value) {
						$permission_name .= $value['permission_name'].",";
					}
					$res[$k]['permission_name'] = trim($permission_name,',');
				}
			}
			$res?$output = $res:$output = [];
			echo json_encode($output);
		}else{
			$id = I("post.id","2");
			$res = D("SysRole")->select_role($id);
			$res1 = D("RolePermission")->get_permiss($id);
			foreach ($res as $k => $v) {
				foreach ($res1 as $key => $value) {
					$permission_name .= $value['permission_name'].",";
				}
				$res[$k]['permission_name'] = trim($permission_name,',');
			}
			$res?$output = $res:$output = [];
			echo json_encode($output);
			// $output['result'] = $this->getState("5");
			// echo json_encode($output);
		}
	}

	public function edit()
	{
		if(IS_POST){
			$id = I("post.id","");
			$role_name = I("post.role","");
			$status = I("post.status","");
			$auth = I("post.auth","");
			// $parent_id = I("post.parent_id","");
			$res1 = D("SysRole")->edit_role($id,$role_name,$status);
			$res = D("Permission")->get_perm_id1($id,$auth);
			// dump($res);exit();
			$res?$output= 1:$output = 0;
			echo json_encode($output);
		}else{
			// $output['result'] = $this->getState("5");
			// echo json_encode($output);
		}
	}
}